<!DOCTYPE html>
<html>
<head>
  <title>猜数字游戏</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      text-align: center;
      padding: 20px;
    }
    input, button {
      padding: 10px;
      font-size: 16px;
      margin: 5px;
    }
    #message {
      font-size: 18px;
      margin-top: 20px;
      font-weight: bold;
    }
    .success { color: green; }
    .error { color: red; }
  </style>
</head>
<body>
  <h1>猜数字游戏</h1>
  <p>我想了一个 1 - 100 之间的数字，你能猜出来吗？</p>
  <input type="number" id="guessInput" min="1" max="100" placeholder="输入数字">
  <button id="guessButton">猜一猜</button>
  <p id="message"></p>
  <p id="attempts">尝试次数：0</p>
  <script>
    const secretNumber = Math.floor(Math.random() * 100) + 1;
    let attempts = 0;
    let gameOver = false;

    const guessInput = document.getElementById('guessInput');
    const guessButton = document.getElementById('guessButton');
    const message = document.getElementById('message');
    const attemptsElement = document.getElementById('attempts');

    function checkGuess() {
      if (gameOver) return;
      
      const userGuess = parseInt(guessInput.value);
      attempts++;
      attemptsElement.textContent = `尝试次数：${attempts}`;

      if (isNaN(userGuess) || userGuess < 1 || userGuess > 100) {
        message.textContent = '请输入 1 - 100 之间的数字！';
        message.className = 'error';
        return;
      }

      if (userGuess === secretNumber) {
        message.textContent = `恭喜！正确答案是 ${secretNumber}，你用了 ${attempts} 次猜对！`;
        message.className = 'success';
        gameOver = true;
        guessButton.disabled = true;
      } else if (userGuess < secretNumber) {
        message.textContent = '猜小了！再大一点...';
        message.className = 'error';
      } else {
        message.textContent = '猜大了！再小一点...';
        message.className = 'error';
      }
    }

    guessButton.addEventListener('click', checkGuess);
    guessInput.addEventListener('keypress', (e) => {
      if (e.key === 'Enter') checkGuess();
    });
  </script>
</body>
</html>